package org.millioncall.yueyoga.admin.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.CheduleDAO;
import org.millioncall.yueyoga.admin.dao.entity.ScheduleEntity;
import org.millioncall.yueyoga.admin.model.Chedule;
import org.millioncall.yueyoga.admin.model.ClassScheduleModel;

public class CheduleDAOImpl extends BaseDao implements CheduleDAO {

	@Override
	public int deleteByPrimaryKey(Integer id) {
		return this.getPagableSqlSessionTemplate().delete(
				"deleteCheduleByPrimaryKey", id);
	}

	@Override
	public int insert(Chedule record) {
		return this.getPagableSqlSessionTemplate().insert("insertChedule",
				record);
	}

	@Override
	public int insertSelective(Chedule record) {
		return this.getPagableSqlSessionTemplate().insert(
				"insertCheduleSelective", record);
	}

	@Override
	public Chedule selectByPrimaryKey(Integer id) {
		return this.getPagableSqlSessionTemplate().selectOne(
				"selectCheduleByPrimaryKey", id);
	}

	@Override
	public int updateByPrimaryKeySelective(Chedule record) {
		return this.getPagableSqlSessionTemplate().update(
				"updateCheduleByPrimaryKeySelective", record);
	}

	@Override
	public int updateByPrimaryKey(Chedule record) {
		return this.getPagableSqlSessionTemplate().update(
				"updateCheduleByPrimaryKey", record);
	}

	@Override
	public List<Chedule> findBySubjectId(Integer subjectId) {
		PreparedQuery<Chedule> pq = this.getPagableSqlSessionTemplate()
				.findAll("findCheduleByClassId", subjectId);
		List<Chedule> list = pq.list();
		return list;
	}

	@Override
	public int updateOrInsert(Chedule record) {
		return this.getPagableSqlSessionTemplate().update(
				"updateOrInsertChedule", record);
	}

	@Override
	public PaginatedList<Chedule> getCheduleInPage(int start, int pageSize,
			Map<String, Object> map) {
		try {
			PreparedQuery<Chedule> pq = this.getPagableSqlSessionTemplate()
					.findAll("findChedulePage", map);
			PaginatedList<Chedule> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, pageSize)));
			return pl;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public int updateClassChedule(Map<String, Object> map) {

		return this.getPagableSqlSessionTemplate().insert("updateClassChedule",
				map);
	}

	@Override
	public Chedule getCheduleById(Integer cheduleId) {
		Chedule chedule = null;
		try {
			chedule = this.getPagableSqlSessionTemplate().selectOne(
					"selectCheduleById", cheduleId);
		} catch (Exception e) {
			throw e;
		}
		return chedule;
	}

	@Override
	public int deleteScheduleByClassIds(List<Integer> ids) {
		return this.getPagableSqlSessionTemplate().delete(
				"deleteSchedulesByClassIds", ids);
	}

	@Override
	public boolean saveScheduleEntity(ScheduleEntity entity) {
		return this.getPagableSqlSessionTemplate().insert("insertSchedule",
				entity) > 0;
	}

	@Override
	public List<ScheduleEntity> findByClassId(Map<String, Object> map) {
		PreparedQuery<ScheduleEntity> pq = this.getPagableSqlSessionTemplate()
				.findAll("findSchedulesEntityByClassId", map);
		return pq.list();
	}

	@Override
	public List<ClassScheduleModel> findClassSchedulesByClsId(Integer clsId,
			Integer week, String date) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("classId", clsId);
		map.put("week", week);
		map.put("cheduleTime", date);
		return this.getPagableSqlSessionTemplate().selectList(
				"findClassSchedulesByClsId", map);
	}

	@Override
	public PaginatedList<Chedule> findConsumptionSchedule(
			Map<String, Object> map, Integer start, Integer limit) {
		PreparedQuery<Chedule> pq = this.getPagableSqlSessionTemplate()
				.findAll("findConsumptionSchedule", map);
		PaginatedList<Chedule> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public Chedule findConsumptionNewById(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectOne(
				"findConsumptionNewById", map);
	}

	@Override
	public List<ClassScheduleModel> findScheduleListByCdts(
			Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList(
				"findScheduleListByCdts", map);
	}

	@Override
	public int updateScheduleOver(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().update(
				"updateScheduleOverFirst", map);
	}

	@Override
	public int updateScheduleReserve(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().update(
				"updateScheduleReserve", map);
	}

	@Override
	public List<Chedule> findAllScheduleOneReserve(Map<String, Object> map) {
		PreparedQuery<Chedule> pq = this.getPagableSqlSessionTemplate()
				.findAll("findAllScheduleOneReserve", map);
		return pq.list();
	}

	@Override
	public Set<String> findAllWeekClassIds(int week,Date aDay) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("week", week);
		map.put("date", aDay);
		List<String> result = this.getPagableSqlSessionTemplate().selectList(
				"findAllWeekClassIds", map);
		return new HashSet<String>(result);
	}

	@Override
	public Set<String> findAllDateClassIds(Date aDay) {
		List<String> result = this.getPagableSqlSessionTemplate().selectList(
				"findAllDateClassIds", aDay);
		return new HashSet<String>(result);
	}

}
